<template>
    <div>
        <el-row :gutter="20">
            <template v-if="panels.length == 0">
                <el-col :span="6" v-for="i in 4" :key="i">
                    <el-skeleton style="width: 100%;" animated loading>
                        <template #template>
                            <el-card shadow="hover" class="border-0">
                                <template #header>
                                    <div class="flex justify-between">
                                        <el-skeleton-item variant="text" style="width: 50%"/>
                                        <el-skeleton-item variant="text" style="width: 10%"/>
                                    </div>
                                </template>
                                <el-skeleton-item variant="h3" style="width: 80%"/>
                                <el-divider/>
                                <div class="flex justify-between text-sm text-gray-500">
                                    <el-skeleton-item variant="text" style="width: 50%"/>
                                    <el-skeleton-item variant="text" style="width: 10%"/>
                                </div>
                            </el-card>
                        </template>
                    </el-skeleton>
                </el-col>
            </template>


            <el-col :span="6" :offset="0" v-for="(item, index) in panels" :key="index">
                <el-card shadow="hover" class="border-0">
                    <template #header>
                        <div class="flex justify-between">
                            <span class="text-sm">{{ item.title }}</span>
                            <el-tag :type="item.unitColor" effect="plain">
                                {{ item.unit }}
                            </el-tag>
                        </div>
                    </template>
                    <span class="text-3xl font-bold text-gray-500">
                        <CountTo :value="item.value"/>
                    </span>
                    <el-divider/>
                    <div class="flex justify-between text-sm text-gray-500">
                        <span>{{ item.subTitle }}</span>
                        <span>{{ item.subValue }}</span>
                    </div>
                </el-card>

            </el-col>
        </el-row>

        <IndexNavs/>

        <el-row :gutter="20" class="mt-5">
            <el-col :span="12" :offset="0">
                <IndexChart/>
            </el-col>
            <el-col :span="12" :offset="0">
                <IndexCard title="在线微课" tip="在线微课统计" :btns="periods" class="mb-3"/>
                <IndexCard title="用户管理" tip="用户人数统计" :btns="users"/>
            </el-col>
        </el-row>
    </div>
</template>
<script setup>
    import {ref,onMounted} from "vue"
    import CountTo from "~/components/CountTo.vue";
    import IndexNavs from "~/components/IndexNavs.vue";
    import IndexChart from "~/components/IndexChart.vue";
    import IndexCard from "~/components/IndexCard.vue";
    import {getStatistics, getStatistics2} from "~/api/index.js"
    import request from "~/utils/request.js";

    // const panels = ref([])
    // getStatistics()
    //     .then(res=>{
    //         panels.value = res.panels
    //         console.log(panels.value);
    //     })

    // const goods = ref([])
    // const order = ref([])
    // getStatistics2().then(res=>{
    //     goods.value = res.goods
    //     order.value = res.order
    // })

    const panels = ref([])


    const periods = ref([])
    const users = ref([])

    // 加载数据
    const fetchData = async () => {
        try {
            request.get('/course/findIndexInfo').then(res=>{
                if(res.code === "200"){
                    panels.value = res.data.panels
                    periods.value = res.data.periods
                    users.value = res.data.users
                }
            })
        } catch (error) {
            console.error('Error fetching data:', error);
        }
    };

    // 在组件挂载后加载数据
    onMounted(() => {
        fetchData();
    });
</script>
